/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-06 09:51:29  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.controller;

import com.google.gson.Gson;
import com.joysuch.wwyt.bp.bean.BpRegulationDocumentAddBean;
import com.joysuch.wwyt.bp.bean.BpRegulationDocumentEditBean;
import com.joysuch.wwyt.bp.bean.BpRegulationDocumentListBean;
import com.joysuch.wwyt.bp.bean.BpRegulationDocumentListSearchBean;
import com.joysuch.wwyt.bp.entity.BpRegulationDocument;
import com.joysuch.wwyt.bp.enums.RegulationDocumentTypes;
import com.joysuch.wwyt.bp.service.BpRegulationDocumentService;
import com.joysuch.wwyt.core.bean.BaseAttachmentBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 制度规范管理——文档
 */
@RestController
@Api(tags = "制度规范管理—文档")
@RequestMapping("/bp/regulation-document")
public class BpRegulationDocumentController {

    private static final Logger logger = LoggerFactory.getLogger(BpRegulationDocumentController.class);

    @Autowired
    private BpRegulationDocumentService bpRegulationDocumentService;

    /**
     * 添加文档
     * @param regDocAdd     待添加的文档相关属性
     * @param regDocAdd attachmentJson    附件相关属性 JSON 字符串
     */
    @PostMapping("/add")
    @ApiOperation(value = "保存文档" )
    public Object add(@RequestBody BpRegulationDocumentAddBean regDocAdd) {
        String attachmentJson = regDocAdd.getAttachmentJson();
        try {
//            if (attachmentJson == null) {
//                return ResultBean.fail(102, "未传入附件");
//            }
//            Gson gson = new Gson();
//            BaseAttachmentBean attachment = gson.fromJson(attachmentJson, BaseAttachmentBean.class);
//            if (attachment.getId() == null) {
//                return ResultBean.fail(102, "未传入附件");
//            }
            if (attachmentJson != null && attachmentJson.length() > 0) {
                Gson gson = new Gson();
                BaseAttachmentBean attachment = gson.fromJson(attachmentJson, BaseAttachmentBean.class);
                regDocAdd.setAttachmentId(attachment==null?null:attachment.getId());
            }
            bpRegulationDocumentService.add(regDocAdd);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    /**
     * 修改文档
     * @param regDocEdit    待修改的文档相关属性
     * @param regDocEdit attachmentJson    附件相关属性 JSON 字符串（可不传）
     */
    @PostMapping("/update")
    @ApiOperation(value = "保存文档" )
    public Object update(@RequestBody BpRegulationDocumentEditBean regDocEdit) {
        String attachmentJson = regDocEdit.getAttachmentJson();
        try {
            if (regDocEdit.getDocId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            if (attachmentJson != null) {
                Gson gson = new Gson();
                BaseAttachmentBean attachment = gson.fromJson(attachmentJson, BaseAttachmentBean.class);
                regDocEdit.setAttachmentId(attachment==null?null:attachment.getId());
            }
            bpRegulationDocumentService.update(regDocEdit);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#"+regDocEdit.getDocId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    /**
     * 获取文档信息（分页）
     * @param page  分页信息
     * @param regDocListSearch  筛选条件
     */
    @RequestMapping("page")
    public Object page(Pageable page, BpRegulationDocumentListSearchBean regDocListSearch) {
        try {
            Page<BpRegulationDocumentListBean> data = bpRegulationDocumentService.findByPage(page, regDocListSearch);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    /**
     * 删除文档
     * @param id    指定文档 ID
     */
    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpRegulationDocumentService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    /**
     * 提交审核
     * @param id    指定文档 ID
     */
    @RequestMapping("submitAudit")
    public Object submitAudit(Long id) {
        try {
            BpRegulationDocument tempDoc = bpRegulationDocumentService.findById(id);
            if (tempDoc.getState() != RegulationDocumentTypes.TO_BE_AUDITED.getId()) {
                return ResultBean.fail(102, "文档状态错误");
            }
            bpRegulationDocumentService.changeState(id, null, null);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("提交审核失败", e);
            return ResultBean.fail(101, "提交审核失败");
        }
    }

    /**
     * 审核[通过/不通过]
     * @param id    指定审核文档 ID
     * @param isPass    是否通过（true:通过；false:不通过）
     * @param auditRemark   审核意见
     */
    @RequestMapping("audit")
    public Object audit(Long id, Boolean isPass, String auditRemark) {
        try {
            BpRegulationDocument tempDoc = bpRegulationDocumentService.findById(id);
            if (tempDoc.getState() != RegulationDocumentTypes.AUDIT_WAIT.getId()) {
                return ResultBean.fail(102, "文档状态错误");
            }
            bpRegulationDocumentService.changeState(id, isPass, auditRemark);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("审核操作失败", e);
            return ResultBean.fail(101, "审核操作失败");
        }
    }
}
